package com.skyvin.nreceipts.admin.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skyvin.nreceipts.admin.mongorepository.RetailerTemplateRepository;
import com.skyvin.nreceipts.admin.service.RetailerTemplateService;
import com.skyvin.nreceipts.common.document.RetailerTemplate;
import com.skyvin.nreceipts.common.dto.RetailerDTO;
import com.skyvin.nreceipts.common.mapper.RetailerMapper;
import com.skyvin.nreceipts.common.value.JQGridResponse;
import com.skyvin.nreceipts.common.value.MessageValue;

@Controller
@RequestMapping(value = "retailerTemplate")
public class RetailerTemplateController {
	private static Logger logger = Logger.getLogger(RetailerTemplateController.class);

	@Autowired
	private RetailerTemplateRepository retailerTemplateRepository;
	
	@Autowired
	private RetailerTemplateService retailerTemplateService;

	@RequestMapping(value = "/loadRetailerPage")
	 @PreAuthorize("hasAuthority('ADMIN')")
	public String load() {
		return "admin/retailerTemplateEdit";
	}
	
	@RequestMapping(value="/viewRetailers", method=RequestMethod.GET)
	@ResponseBody
	public JQGridResponse viewRetailers(@RequestParam("_search") Boolean search,
    		@RequestParam(value="filters", required=false) String filters,
    		@RequestParam(value="page", required=false) Integer page,
    		@RequestParam(value="rows", required=false) Integer rows,
    		@RequestParam(value="sidx", required=false) String sidx,
    		@RequestParam(value="sord", required=false) String sord) {
		
		List<RetailerDTO> retailerDTOList = null;
		Page<RetailerTemplate> retailerPageList = null;
		
		JQGridResponse jqGridResponse = new JQGridResponse();
		Pageable pageRequest = new PageRequest(page-1, rows);
		
		retailerPageList = retailerTemplateRepository.findAll(pageRequest);
		retailerDTOList = RetailerMapper.map(retailerPageList);
		
		jqGridResponse.setPage(1);
		
		if(retailerPageList!= null) {
			
			jqGridResponse.setRecords(retailerPageList.getTotalElements());
			jqGridResponse.setTotal(retailerPageList.getTotalPages());
			jqGridResponse.setPage(retailerPageList.getNumber()+1);
		}
		jqGridResponse.setRows(retailerDTOList);
		return jqGridResponse;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('ADMIN')")
	public @ResponseBody MessageValue  save(RetailerTemplate retailer) {
		MessageValue message = new MessageValue();

		if (retailer.getId() != null
				&& retailer.getId().equals("_empty")) {
			retailer.setId(null);
		}

		retailerTemplateService.save(retailer);

		message.setMessage("Retailer template " + retailer.getValue() + " added");
		// message.setDetailedMessage("http://farm4.staticflickr.com/3510/3208351775_e06db18ee4.jpg");

		return message;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('ADMIN')")
	public @ResponseBody
	MessageValue delete(@RequestParam("oper") String oper,
			@RequestParam("id") String pid) {
		MessageValue message = new MessageValue();
		retailerTemplateService.delete(pid);
		message.setMessage("Retailer deleted");
		return message;
	}
	@RequestMapping(value="/searchRetailers", method=RequestMethod.GET)
	@ResponseBody
	public List<RetailerTemplate> searchRetailers(@RequestParam(value="term", required=false) String searchTerm) {
		List<RetailerTemplate> retailerCategories = retailerTemplateRepository.findByValueLike(searchTerm);
		return retailerCategories;
	}
}
